package com.ph.irp.base.settlement.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ph.irp.base.accountbook.dao.PBaseAccountbookDAO;
import com.ph.irp.base.settlement.dao.PBaseSettlementDAO;
import com.ph.irp.base.settlement.service.PBaseSettlementService;
import com.ph.irp.dto.SettlementDTO;
import com.ph.irp.model.PBaseAccountbook;
import com.ph.irp.model.PBaseSettlement;

public class PBaseSettlementServiceImpl implements PBaseSettlementService {
	
	//结算方式dao
	private PBaseSettlementDAO settlementProxy = null ;
	
	//结算方式dtolist
	private List<SettlementDTO> settlementDTOList = null ;
	//结算方式modellist
	private List<PBaseSettlement> settlementList = null ;

	//会计科目dao
	private PBaseAccountbookDAO accountbookDAOProxy = null ;
	//会计科目model
	private PBaseAccountbook pbAccountbook = null ;
	/**
	 * 删除结算方式-可删除多条
	 */
	@Override
	public void deleteSettlements(Object[] settlementIds)throws Throwable {
		// TODO Auto-generated method stub
		
		settlementProxy.deleteSettlementsById(settlementIds);
	}
	/**
	 * 根据id查询结算方式
	 */
	@Override
	public PBaseSettlement getSettlementById(Integer settlementId) throws Throwable{
		
		return settlementProxy.getSettlementById(settlementId);
		
	}
	/**
	 * 动态查询会计科目
	 */
	@Override
	public List<SettlementDTO> getSettlements(PBaseSettlement pbaseSettlement)throws Throwable {
		// TODO Auto-generated method stub
		settlementDTOList = new ArrayList<SettlementDTO>();
		
		if(pbaseSettlement==null){
			settlementList = this.settlementProxy.getAllSettlements();
		}
		else{
			//结算方式name
			String name = pbaseSettlement.getSettlementName().trim();
			//结算方式code
			String code = pbaseSettlement.getSettlementCode().trim();
			if(!code.equals("")&&!name.equals("")){			
				settlementList = this.settlementProxy.getSettlementsByNameAndCode(name,code);				
			}
			else if(!code.equals("")){
				
				settlementList = this.settlementProxy.getSettlementsByCode(code) ;						
			}
			else if(!name.equals("")){
				settlementList = this.settlementProxy.getSettlementsByName((name)) ;
			}
			else {
				settlementList = this.settlementProxy.getAllSettlements() ;
			}
		}
		settlementDTOList = this.convertList(settlementList, settlementDTOList) ;
		return settlementDTOList;
	}
	
	/**
	 * 新增结算方式
	 */
	@Override
	public void addSettlement(PBaseSettlement pbaseSettlement) throws Throwable{
		// TODO Auto-generated method stub
		
		settlementProxy.addSettlement(pbaseSettlement);
		
	}
	
	@Override
	public void updateSettlement(PBaseSettlement pbaseSettlement) throws Throwable{
		// TODO Auto-generated method stub
		
		settlementProxy.updateSettlement(pbaseSettlement);
		
	}
	/**
	 * 将结算方式model转化为dto
	 * @param settlementList
	 * @param settlementDTOList
	 * @return
	 * @throws Throwable 
	 */
	private List<SettlementDTO> convertList(List<PBaseSettlement> settlementList,List<SettlementDTO> settlementDTOList) throws Throwable{
		
		int i = 0 ;
		for(i=0;i<settlementList.size();i++){
			
			SettlementDTO settlementdto = new SettlementDTO();
			
			settlementdto.setSettlement(settlementList.get(i));
			
			pbAccountbook = accountbookDAOProxy.getAccountbookById(settlementList.get(i).getPBaseAccountbookId()) ;
		
			settlementdto.setPbaccountbook(pbAccountbook) ;
			
			settlementDTOList.add(settlementdto) ;
		
		}
		
		return settlementDTOList ;
	}

	public PBaseSettlementDAO getSettlementProxy() {
		return settlementProxy;
	}
	public void setSettlementProxy(PBaseSettlementDAO settlementProxy) {
		this.settlementProxy = settlementProxy;
	}
	/**
	 * @return the accountbookDAOProxy
	 */
	public PBaseAccountbookDAO getAccountbookDAOProxy() {
		return accountbookDAOProxy;
	}
	/**
	 * @param accountbookDAOProxy the accountbookDAOProxy to set
	 */
	public void setAccountbookDAOProxy(PBaseAccountbookDAO accountbookDAOProxy) {
		this.accountbookDAOProxy = accountbookDAOProxy;
	}
	



}
